//
//  FRQMyWalletsViewController.m
//  FRQBluetoothKit_Example
//
//  Created by admin on 2023/10/24.
//  Copyright © 2023 xuchunhai. All rights reserved.
//

#import "FRQMyWalletsViewController.h"
#import "FRQRemoveSelectedWalletViewController.h"
#import "FRQWalletVerificationViewController.h"
#import "MyWalletsTableViewCell.h"
#import "NoWalletsYetViews.h"
@interface FRQMyWalletsViewController ()
@property (nonatomic,strong) NSArray *arrwallet;
@property (nonatomic,strong) UIButton *btnVerify;
@property (nonatomic,strong) NoWalletsYetViews *NoWalletsYetView;
@end

@implementation FRQMyWalletsViewController
- (NoWalletsYetViews *)NoWalletsYetView{
    if (nil==_NoWalletsYetView) {
        _NoWalletsYetView=skXibView(@"NoWalletsYetViews");
        [self.view addSubview:_NoWalletsYetView];
        [_NoWalletsYetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_topLayoutGuide);
            make.bottom.mas_equalTo(self.mas_bottomLayoutGuide);
            make.right.left.mas_equalTo(0);
        }];
        @weakify(self)
        [[_NoWalletsYetView.btnVerify rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            FRQWalletVerificationViewController *view=[[FRQWalletVerificationViewController alloc] init];
            [self.navigationController pushViewController:view animated:YES];
        }];
    }
    return _NoWalletsYetView;
}
- (UIButton *)btnVerify{
    if (nil==_btnVerify) {
        _btnVerify=[[UIButton alloc] initWithFrame:CGRectMake(45, 80, skScreenWidth-90, 42)];
        _btnVerify.backgroundColor=skphUIColorFromRGB(0x44cd2c,0.8);
        [_btnVerify setTitle:@"Verify New Wallet" forState:(UIControlStateNormal)];
        [_btnVerify skSetBoardRadius:5 Width:0 andBorderColor:[UIColor clearColor]];
        @weakify(self)
        [[_btnVerify rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            FRQWalletVerificationViewController *view=[[FRQWalletVerificationViewController alloc] init];
            [self.navigationController pushViewController:view animated:YES];
            
        }];
    }
    return _btnVerify;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"My Wallets";
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self appUserWalletList];
}
-(void)appUserWalletList{
    
    NSDictionary *dic=@{};
    
    
    [skNetTools skGETWithURL:[NSString stringWithFormat:@"%@%@",skURL,@"app/user/wallet/list"] parameters:dic cachePolicy:0 callback:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        NSLog(@"数据=%@",responseObject);
        skNetModel *model=[skNetModel mj_objectWithKeyValues:responseObject];;
        if(model.code==200){
            self.arrwallet=model.data;
            if(self.arrwallet.count>0){
                [self addTableView];
                self.tableView.hidden=NO;;
                self.NoWalletsYetView.hidden=YES;
                [self.tableView reloadData];
            }else{
                self.NoWalletsYetView.hidden=NO;
                self.tableView.hidden=YES;
            }
        }else{
            [SkToast SkToastShow:model.msg];
        }
    }];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
-(void)addTableView{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_topLayoutGuideBottom);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.mas_bottomLayoutGuideTop);
    }];
    self.tableView.backgroundColor=skUIColorFromRGB(0x1c1f2a);
}
#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrwallet.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 80;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc] init];
    view.frame=CGRectMake(0, 0, skScreenWidth, 80);
    UILabel *lab=[[UILabel alloc] initWithFrame:CGRectMake(45, 10, 200, 30)];
    lab.text=@"Verified Wallets";
    [view addSubview:lab];
    lab.textColor=[UIColor whiteColor];
    
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 120;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view=[[UIView alloc] init];
    view.frame=CGRectMake(0, 0, skScreenWidth, 120);
    [view addSubview:self.btnVerify];
    return view;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"MyWalletsTableViewCell";
    MyWalletsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = skXibView(@"MyWalletsTableViewCell");
        @weakify(self)
        [[cell.btnRemove rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            
            NSDictionary *dic1=[self.arrwallet objectAtIndex:indexPath.row];
            NSString *wid=[dic1 objectForKey:@"id"];
            
            FRQRemoveSelectedWalletViewController *view=[[FRQRemoveSelectedWalletViewController alloc] init];
            view.modalPresentationStyle=0;
            view.walletId=wid;
            [self presentViewController:view animated:YES completion:^{
                
            }];
            
            
            
            
        }];
    }
    cell.contentView.backgroundColor=skUIColorFromRGB(0x1c1f2a);
    NSDictionary *dic=[self.arrwallet objectAtIndex:indexPath.row];
    
    NSString *walletAddress=[dic objectForKey:@"walletAddress"];
    cell.txtWallet.text=walletAddress;
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
